<template>
    <div>
        <div class="title">
            <div class="back" @click="$router.back()">
                <span> <img src="../assets/img/fanhui.png" alt=""></span>
            </div>
            <div class="gamename">我的订单</div>
        </div>
        <div class="carlist">
            <div class="item active">全部订单</div>
            <div class="item">未付款</div>
            <div class="item">已完成</div>
        </div>
        <div v-if="userOrderList.length == 0" class="empty">
            <p> 你的订单空荡荡，快起下单吧~</p>
        </div>
        <div v-for="(item, index) in userOrderList" :key="index">

            <div v-if="item.length == 1" class="order">
                <div class="state">
                    <p> 已完成</p>
                    <p @click="delDetail(index)">删除订单</p>
                </div>
                <div class="content">
                    <img :src="item[0].img" alt="">
                    <p>{{ item[0].name }}</p>

                </div>
                <p> <span>订单完成时间:{{ item[0].iscoupon }}</span> <span>共付款￥{{ item[0].price }}</span> </p>
            </div>
            <div class="order" v-else>
                <div class="state">
                    <p> 已完成</p>
                    <p @click="delDetail(index)">删除订单</p>
                </div>
                <div class="content">
                    <img v-for="(data, index) in item" :key="index" :src="data.img" alt="">
                </div>
                <p><span>订单完成时间:{{ item[0].iscoupon }}</span> <span>共{{ item.length }}款商品，共付款￥{{ item[0].type }}</span> </p>
            </div>
        </div>
        <div class="dialogbox" v-show="showDialog">
            <div class="dialog">
                <div class="top">
                    <p>确认删除订单吗？</p>
                </div>
                <div class="bottom">
                    <div>
                        <p @click="cancel"> 取消</p>
                    </div>
                    <div>
                        <p @click="del"> 确认</p>
                    </div>
                </div>
            </div>
        </div>
        <div class="blank"></div>
    </div>
</template>
<style lang="scss" scoped>
.dialogbox {
    width: 100%;
    height: 100%;
    background-color: rgba(24, 23, 23, 0.8);
    position: fixed;
    top: 0;
    z-index: 5;
}

.dialog {
    .bottom {
        width: 100%;
        display: flex;
        justify-content: space-between;

        div {
            width: 50%;
            text-align: center;
            display: inline-block;
            border: 1px solid #f6f7f8;
            height: 50px;
            line-height: 50px;

            &:nth-child(1) {
                border-radius: 0 0 0 15px;
            }

            &:nth-child(2) {
                border-radius: 0 0 15px 0;
                color: red;
            }
        }
    }

    display: flex;
    flex-direction: column;
    justify-content: space-between;
    align-items: center;
    position: fixed;
    bottom: 50%;
    top: 50%;
    width: 350px;
    height: 200px;
    background-color: #ffffff;
    left: 50%;
    right: 50%;
    transform: translate(-50%, -50%);
    z-index: 999;
    border-radius: 10px;

    .top {
        p {
            margin-top: 75px;
            font-size: 16px;
        }
    }

}

.empty {
    width: 100%;
    margin: 100px auto;

    p {
        text-align: center;
        color: #e85323;
    }
}

.order {
    border-top: 1px solid #cccccc;
    margin-bottom: 10px;
    border-bottom: 1px solid #cccccc;

    p {
        display: flex;
        font-size: 14px;
        text-align: right;
        padding: 0 20px 5px 20px;
        justify-content: space-between;

        span {
            font-size: 12px;
        }
    }

    .state {
        padding: 10px 5px;
        font-size: 13px;
        color: #999999;
        border-top: 1px solid #cccccc;
        border-bottom: 1px solid #cccccc;
        display: flex;
        justify-content: space-between;

        &:nth-child(1) {
            border-top: 0px solid #cccccc;
        }
    }

    .content {
        display: flex;
        padding: 15px;
        align-items: center;
        flex-wrap: wrap;

        img {
            width: 32%;
            height: 60px;
            margin-right: 4px;
            margin-bottom: 5px;
        }
    }
}

.title {
    position: relative;
    z-index: 2;
    display: flex;
    background-color: #333333;
    width: 100%;
    height: 45px;
    line-height: 45px;
    padding: 0 12px;
    color: #cccccc;
    justify-content: space-between;
    box-sizing: border-box;
    z-index: 2;
    top: 0;
    border-bottom: 2px solid #686868;

    .back {
        display: flex;
        align-items: center;

        span {
            font-size: 16px;

            img {
                width: 16px;
                height: 16px;
            }
        }
    }

    .gamename {
        flex: 1;
        text-align: center;
        font-size: 17px;
    }

    .more {
        span {
            font-size: 17px;
        }
    }



}

.carlist {

    display: flex;
    box-sizing: border-box;
    padding: 10px 30px 0;
    justify-content: space-between;
    background-color: #fff;
    border-bottom: 1px solid #e6e6e6;

    .item {
        margin-top: 5px;
        padding: 0px 15px 10px;

        font-size: 13px;

        &.active {
            color: #e85323;
            border-bottom: 2px solid #e85323;
        }
    }
}
</style>

<script>
import { Toast } from 'vant'

export default {
    data() {
        return {
            // 获取localStorage里面的历史订单记录渲染
            userOrderList: JSON.parse(localStorage.getItem('listOrder')) || [],
            delIndex: null,
            showDialog: false,
        }
    }, methods: {
        delDetail(index) {
            this.showDialog = true
            this.delIndex = index

        },
        cancel() {
            this.showDialog = false
        },
        del() {
            this.showDialog = false
            this.userOrderList.splice(this.delIndex, 1)
            window.localStorage.setItem('listOrder', JSON.stringify(this.userOrderList))
            Toast('已删除订单')
        }
    }
}
</script>